package com.zgjkhis.modules.registration.rest;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.outpatient.domain.Account;
import com.zgjkhis.modules.outpatient.dto.AccountDto;
import com.zgjkhis.modules.outpatient.service.AccountService;
import com.zgjkhis.modules.registration.domain.vo.RegistrationVO;
import com.zgjkhis.modules.registration.service.dto.ConsultDto;
import com.zgjkhis.modules.registration.service.dto.ConsultQueryCriteria;
import com.zgjkhis.modules.registration.service.dto.RegistrationDto;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.system.domain.Consult;
import com.zgjkhis.modules.system.domain.Department;
import com.zgjkhis.modules.system.domain.StoreUser;
import com.zgjkhis.modules.system.domain.User;
import com.zgjkhis.modules.system.service.ConsultService;
import com.zgjkhis.modules.system.service.DepartmentService;
import com.zgjkhis.modules.system.service.StoreUserService;
import com.zgjkhis.modules.system.service.UserService;
import com.zgjkhis.modules.util.CharUtil;
import com.zgjkhis.utils.DateUtils;
import com.zgjkhis.utils.RandomUtils;
import com.zgjkhis.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author yzq
 * @Description 挂号管理
 * @Date 2021/9/6 11:36
 * @Param
 * @return
 **/
@Api(tags = "挂号：挂号信息")
@RestController
@RequestMapping("/api/registration")
@Slf4j
public class RegistrationController {

    private final AccountService accountService;
    private final ConsultService consultService;
    private final StoreUserService storeUserService;
    private final UserService userService;
    private final DepartmentService departmentService;


    public RegistrationController(AccountService accountService, ConsultService consultService, StoreUserService storeUserService, UserService userService, DepartmentService departmentService) {
        this.accountService = accountService;
        this.consultService = consultService;
        this.storeUserService = storeUserService;
        this.userService = userService;
        this.departmentService = departmentService;
    }


    @Log("挂号/修改挂号信息")
    @ApiOperation("挂号/修改挂号信息")
    @PostMapping(value = "/submit")
//    @PreAuthorize("@el.check('registration:add')")
    public ResponseEntity<Object> submit(@Validated @RequestBody RegistrationVO registrationVO) {
        Account account = registrationVO.getAccount();
        account.setPharmacyId(LoginUserUtil.getPharmId());
        accountService.saveOrUpdate(account);

        Consult consult = registrationVO.getConsult();

        StoreUser storeUser = storeUserService.getOne(Wrappers.<StoreUser>query().lambda().eq(StoreUser::getUserId, consult.getConsultDocId()).eq(StoreUser::getStoreId, LoginUserUtil.getPharmId()));
        User user = userService.getById(consult.getConsultDocId());
        if (CharUtil.isEmpty(storeUser) || CharUtil.isEmpty(user)) {
            throw new ServiceException("无效医生");
        }
        consult.setConsultDocName(user.getNickName());
        Department department = departmentService.getById(consult.getConsultDepartId());
        if (CharUtil.isEmpty(department) || department.getStatus() == 0 || (department.getType() != 0 && !department.getCreateUser().equals(LoginUserUtil.getPharmId()))) {
            throw new ServiceException("无效科室");
        }
        consult.setConsultDepartName(department.getDepartmentName());
        consult.setCreateUserId(SecurityUtils.getUserId().toString());
        consult.setCreateUserName(LoginUserUtil.getNickName());
        consult.setPharmacyId(LoginUserUtil.getPharmId());
        consult.setConsultNo(DateUtils.dateTimeNow() + RandomUtils.getRandomNumbers(6));
        consult.setPatientId(account.getId().toString());
        consult.setPatientName(account.getName());
        consultService.saveOrUpdate(consult);
        log.info("诊断信息：" + consult.toString() + "--------------" + new ResponseEntity<>(consult, HttpStatus.OK).toString());
        return new ResponseEntity<>(consult, HttpStatus.OK);
    }

    @Log("分页挂号信息")
    @ApiOperation("分页挂号信息")
    @GetMapping(value = "/getList")
//    @PreAuthorize("@el.check('drug:list')")
    public ResponseEntity<PageResult<ConsultDto>> getList(ConsultQueryCriteria criteria, Pageable pageable) {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        return new ResponseEntity<>(consultService.findAllByPageable(criteria, pageable), HttpStatus.OK);
    }

    @Log("查看单个挂号信息")
    @PostMapping(value = "/detail")
    @ApiOperation("查看单个挂号信息")
    //    @PreAuthorize("@el.check('account:datail')")
    public ResponseEntity<RegistrationDto> getDetail(@RequestBody String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        Consult consult = consultService.getById(id);
        if (CharUtil.isEmpty(consult)) {
            throw new ServiceException("查无此挂号信息");
        }
        AccountDto account = accountService.findDetailById(consult.getPatientId());
        RegistrationDto registrationDto = new RegistrationDto();
        registrationDto.setConsult(consult);
        registrationDto.setAccount(account);
        return new ResponseEntity<>(registrationDto, HttpStatus.OK);
    }

    @Log("删除(取消)挂号信息")
    @PostMapping(value = "/delete")
    @ApiOperation("查看单个挂号信息")
    //    @PreAuthorize("@el.check('account:datail')")
    public ResponseEntity<Object> delete(@RequestBody Long id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        Consult consult = new Consult();
        consult.setPharmacyId(LoginUserUtil.getPharmId());
        consult.setId(id);
        consult.setConsultStatus(4);
        consult.setUpdateUserId(SecurityUtils.getUserId().toString());
        consult.setUpdateUserName(LoginUserUtil.getNickName());
        return new ResponseEntity<>(consultService.updateById(consult), HttpStatus.OK);
    }

}
